﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using CMN.Controllers.Filters;
using CMN.Framework;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Sys_RolesController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回角色表列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
         
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
              IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
            IDataService<Sys_UserEntity> sys_UserSv = ServiceBuilder.BuildSys_UserService();
            int recordNum = 0;
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1;
            qp.PageSize = pageSize;
            if (UserData.GetUserData.U_Type == 0)
            {
                QueryParam qpUser = new QueryParam();
                qpUser.Where = "U_Type=0";
                List<Sys_UserEntity> userList2 = sys_UserSv.List(qpUser, out recordNum);
                qp.Where = string.Concat("R_UserID  in (");
                for (int i = 0; i < userList2.Count; i++)
                {
                    if (i < (userList2.Count - 1))
                    {
                        qp.Where = string.Concat(qp.Where, userList2[i].UserID,",");
                    }
                    else
                    {
                        qp.Where = string.Concat(qp.Where, userList2[i].UserID);
                    }
                }
                if (userList2.Count == 0)
                {
                    qp.Where = string.Concat(qp.Where, "0");
                }
                qp.Where = string.Concat(qp.Where, ")");
            }
            else
            {
                qp.Where = string.Concat("R_UserID=", UserData.GetUserData.UserID);
            }
            List<Sys_RolesEntity> sys_rolesList = Sys_RolesSv.List(qp, out recordNum);
            qp.PageIndex=1;
            qp.PageSize=Int32.MaxValue;
            qp.Orderfld="";
            qp.Where="userId in(";
            for (int i = 0; i < sys_rolesList.Count; i++)
            {
                if (i < (sys_rolesList.Count - 1))
                {
                    qp.Where = string.Concat(qp.Where, sys_rolesList[i].R_UserID, ",");
                }
                else
                    qp.Where = string.Concat(qp.Where, sys_rolesList[i].R_UserID);
            }
            if (sys_rolesList.Count == 0)
            {
                qp.Where = string.Concat(qp.Where, "0");
            }
            qp.Where=string.Concat(qp.Where,")");
            List<Sys_UserEntity> userList = sys_UserSv.List(qp, out recordNum);
            foreach (Sys_RolesEntity role in sys_rolesList)
            {
                foreach (Sys_UserEntity user in userList)
                {
                    if (user.UserID == role.R_UserID)
                    {
                        role.R_UserName = user.U_LoginName;
                    }
                }
            }
            if (actionId == "l")
            {
                returnJson = JsonConvert.SerializeObject(sys_rolesList);
            }
            else
            {
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(sys_rolesList), "}");
            }
           
            return returnJson;
 
        }

        /// <summary>
        /// 查看角色表 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
            int intId=Utils.StringToInt(actionId,0);
            Sys_RolesEntity sys_roles = Sys_RolesSv.Look(intId);
            if (sys_roles != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(sys_roles), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新角色表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Sys_RolesEntity sys_roles = new Sys_RolesEntity();
                sys_roles.DataTable_Action = DataTableAction.Update;
                sys_roles.RoleID = intId;
                sys_roles.R_UserID = Utils.GetUserID;
                sys_roles.R_RoleName = (String)Utils.GetQueryAndFormString("R_RoleName", this, 50,  1 , DataType.Str);
                sys_roles.R_Description = (String)Utils.GetQueryAndFormString("R_Description", this, 255,  0, DataType.Str);
                IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
                if (Sys_RolesSv.Update(sys_roles) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增角色表
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Sys_RolesEntity sys_roles = new Sys_RolesEntity();
            sys_roles.DataTable_Action = DataTableAction.Insert;
            sys_roles.R_UserID = Utils.GetUserID;// (Int32)Utils.GetQueryAndFormString("R_UserID", this, 10, 1, DataType.Int);
            sys_roles.R_RoleName = (String)Utils.GetQueryAndFormString("R_RoleName", this, 50,  1 , DataType.Str);
            sys_roles.R_Description = (String)Utils.GetQueryAndFormString("R_Description", this, 255,  0, DataType.Str);
           IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
           if (Sys_RolesSv.Insert(sys_roles) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除角色表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Sys_RolesEntity sys_roles = new Sys_RolesEntity();
                sys_roles.RoleID = intId;
                IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
                IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
                if (Sys_UserRolesSv.Look(intId) == null)
                {
                    if (Sys_RolesSv.Delete(sys_roles) == "")
                    {
                        returnJson = "{success:'true'}";
                    }
                    else
                        returnJson = "{success:'false'}";
                }
                else
                {
                    returnJson = "{success:'false',message:'请先删除该角色下的用户'}";
                }
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除角色表
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
                if (Sys_RolesSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
